package com.blt.govdata.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.blt.govdata.mapper.TProjectMapper;
import com.blt.govdata.mapper.TProjectYearMapper;
import com.blt.govdata.model.TProject;
import com.blt.govdata.model.TProjectYear;
import com.blt.govdata.model.vo.TProjectVo;
import com.blt.govdata.service.ITProjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 * @author AustinGJ123
 * @since 2018-03-26
 */
@Service
public class TProjectServiceImpl extends ServiceImpl<TProjectMapper, TProject> implements ITProjectService {

    @Autowired
    private TProjectMapper mapper;

    @Autowired
    private TProjectYearMapper yearMapper;

    public TProjectVo selectVoById(String id) throws Exception {
        return null;
    }

    public Page<TProjectVo> selectVoPage(Map<String, Object> paramMap) throws Exception {
        //todo
        return null;
    }

    @Override
    public List<Map<String, Object>> showProject(Map<String, Object> params) {
        return this.mapper.showProject(params);
    }

    @Override
    public Page<TProjectVo> selectProjectVoPage(Page<TProjectVo> page, Map<String, Object> params) {
        return this.mapper.selectProjectVoPage(page,params);
    }

    @Override
    public TProjectVo selectProjectBasicInfoById(Map<String, Object> params) {
        return this.mapper.selectProjectBasicInfoById(params);
    }

    @Override
    public boolean editProjectBasicInfo(TProjectVo tProjectVo) {
        TProject t = BeanUtils.instantiate(TProject.class);
        BeanUtils.copyProperties(tProjectVo,t);
        TProjectYear r = BeanUtils.instantiate(TProjectYear.class);
        r.setYinvestment(tProjectVo.getYinvestment());
        r.setProjectProgress(tProjectVo.getProjectProgress());
        int a = this.mapper.updateById(t);
        Integer year = null == tProjectVo.getCurrentYear()?Calendar.getInstance().get(Calendar.YEAR):tProjectVo.getCurrentYear();

        List<TProjectYear> list = this.yearMapper.selectList(BeanUtils.instantiate(EntityWrapper.class).where("year = {0}",year).and("p_id = {0}",tProjectVo.getId()));
        int b = 0;
        if(CollectionUtils.isEmpty(list)){
            r.setYear(year);
            r.setpId(tProjectVo.getId());
            b = this.yearMapper.insert(r);
        }else{
            b = this.yearMapper.update(r,BeanUtils.instantiate(EntityWrapper.class).where("p_id = {0}",tProjectVo.getId())
                    .and("year = {0}",year));
        }
        return (a>0)&&(b>0);
    }

    @Override
    public Page<Map<String, Object>> selectGeometryPage(Map<String, Object> params, Page page) {
        return this.mapper.selectGeometryPage(params,page);
    }

    @Override
    public List<Map<String, Object>> selectSQL(String sql) {
        return this.mapper.selectSQL(sql);
    }

    @Override
    public List<Integer> selectUserBtns(Integer userId) {
        return this.mapper.selectUserBtns(userId);
    }
}
